package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.Book;
import com.lideru.backstage.entity.Bookrack;
import com.lideru.backstage.mapper.BookMapper;
import com.lideru.backstage.mapper.BookrackMapper; 
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/bookrack")
public class BookrackController {
    @Resource
    BookrackMapper bookrackMapper;
    @Resource
    private BookMapper bookMapper;

    @PostMapping
    public Result<?> save(@RequestBody Bookrack br , HttpServletRequest request) {
        Bookrack b = bookrackMapper.selectOne(Wrappers.< Bookrack>lambdaQuery() .eq( Bookrack::getBookId, br.getBookId()).
                eq( Bookrack::getUserId, JWTUtils.getUserId(request.getHeader("token")) ));
        if (b!=null){
            Book book = bookMapper.selectById(b.getBookId());
            if (book!=null){
                b.setName(book.getName());
                b.setCover(book.getCover());
                b.setGmtModified(new Date());
                b.setUserId(JWTUtils.getUserId(request.getHeader("token")) );
                bookrackMapper.updateById(b);
                return Result.success("已经再数书架啦！");
            }else {
                return Result.error("-1","加入书架失败！");
            }

        }else {
            Book book = bookMapper.selectById(br.getBookId());
            if (book!=null){
                Bookrack bookrack = new Bookrack();
                bookrack.setCover(book.getCover());
                bookrack.setName(book.getName());
                bookrack.setBookId(book.getId());
                bookrack.setUserId(JWTUtils.getUserId(request.getHeader("token")));
                bookrack.setGmtCreate(new Date());
                bookrack.setGmtModified(new Date());
                int i = bookrackMapper.insert( bookrack);
                if (i<1){
                    return Result.error("-1","加入书架失败！");
                }
                return Result.success("加入成功");
            }else {
                return Result.error("-1","加入书架失败！");
            }

        }


    }

    @PutMapping
    public Result<?> update(@RequestBody Bookrack Bookrack) {
        Bookrack.setGmtModified(new Date());
        int i = bookrackMapper.updateById( Bookrack);
        if (i<1){
            return Result.error("-1","修改书本类型失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id ) {
        Bookrack bookrack = bookrackMapper.selectOne(Wrappers.< Bookrack>lambdaQuery()
                .eq( Bookrack::getId, id));
        if ( bookrack != null) {
            bookrackMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping("getAllBookrack")
    public Result<?>  getAllBookrack(){
        return Result.success( bookrackMapper.selectList(null))  ;
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search , HttpServletRequest request) {
        return Result.success(
                bookrackMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.< Bookrack>lambdaQuery().
                        eq( Bookrack::getUserId,JWTUtils.getUserId(request.getHeader("token")) )
                        .like( Bookrack::getName, search)));
    }

    /**
     * 统计数据
     *
     * @return
     */
    @GetMapping("/count")
    public Result<?> count(HttpServletRequest request) {
//        User user = getUser(); // 当前登录的用户信息
        return Result.success(bookrackMapper.countBookrack( JWTUtils.getUserId(request.getHeader("token"))));
    }

}
